#ifndef CSE168_BVH_H_INCLUDED
#define CSE168_BVH_H_INCLUDED

#include "Miro.h"
#include "Object.h"


class BVH : public Object
{
public:
	BVH(int depth=0);
	virtual ~BVH();
	void clean();
    void build(Objects * objs);

	virtual int getType() { return TYPE_BVH; }

    bool intersect(HitInfo& result, const Ray& ray,
                   float tMin = 0.0f, float tMax = MIRO_TMAX);

protected:

	Vector3 m_min, m_max;
    Objects * m_objects;
	int m_depth;

	bool bbox_intersect(const Ray& ray) const;
	bool split_box(std::vector<Object *> *objs, int axis, int nbins, float *cost,
					std::vector<Object *> *objs1, std::vector<Object *> *objs2);
	
	static void getBounds(Objects * objs, Vector3 *bmin, Vector3 *bmax);
	static float computeCost(Objects * objs);
};

#endif // CSE168_BVH_H_INCLUDED
